package com.zhuhjay.ucenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuhjay.commonutils.JwtUtils;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.commonutils.ordervo.MemberOrder;
import com.zhuhjay.ucenter.entity.Member;
import com.zhuhjay.ucenter.entity.vo.RegisterVo;
import com.zhuhjay.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-08
 */
@Api(description = "会员用户控制器")
@RestController
@RequestMapping("/educenter/member")

public class MemberController {

    @Resource
    private MemberService memberService;

    @ApiOperation("会员登录")
    @PostMapping("login")
    public R login(@RequestBody Member member) {
        return R.ok().data("token", memberService.login(member));
    }

    @ApiOperation("注册会员")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getLoginInfo(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        return R.ok().data("userInfo", memberService.getInfoById(id));
    }

    @ApiOperation("order微服务获取会员信息")
    @PostMapping("getMemberInfoOrder/{id}")
    public MemberOrder getMemberInfoOrder(@PathVariable String id) {
        Member member = memberService.getById(id);
        MemberOrder memberOrder = new MemberOrder();
        BeanUtils.copyProperties(member, memberOrder);
        return memberOrder;
    }

    @ApiOperation("查询某一天注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        return R.ok().data("countRegister", memberService.getRegisterCountByDay(day));
    }

}

